package com.zhongge.user;

import com.zhongge.book.Book;
import com.zhongge.book.BorrowInfo;
import com.zhongge.book.Library;
import com.zhongge.constant.Constant;
import com.zhongge.utils.AnalyzingBorrowBook;

import java.util.Scanner;

/**
 * @ClassName NormalUser
 * @Description TODO 普通用户
 * @Author zhongge
 * @Version 1.0
 */
@SuppressWarnings("all")
public class NormalUser extends User{
    //聚合借阅信息
    private BorrowInfo[] borrowInfos;
    //当前的借阅量
    private int borrowCount;

    Library library = Library.getLibraryInstance();

    Scanner scanner = new Scanner(System.in);

    //一个用户只能最多借阅5本书
    private static final int MAX_BORROW_COUNT = 5;
    public NormalUser(String name, int userID) {
        super(name, userID, "普通用户");
        //对借阅信息的初始化
        loadAllBorrowInfo();//加载所有的借阅信息
    }

    /**
     * 加载所有的借阅信息
     */
    private void loadAllBorrowInfo() {
        //这个方法的实现方式与Library中的loadAllBook一样的
        //首先得有一个books数组用于存放到时候在文件中加载进来的借阅信息
        borrowInfos = new BorrowInfo[Constant.DEFAULT_CAPACITY];//默认信息也是5
        borrowCount = 0;
        //由于加载数据他是可能返回null的、此时用一个临时变量tmpBooks接受后做判断
        BorrowInfo[] tmpBorrows = AnalyzingBorrowBook.loadBorrowInfo(Constant.BORROW_INFO_FILE_NAME);
        if (tmpBorrows == null) {
            return;
        } else {
            if (tmpBorrows.length > 5) {
                borrowInfos = new BorrowInfo[tmpBorrows.length];
                //将数据拷贝进去
                for (int i = 0; i < tmpBorrows.length; i++) {
                    borrowInfos[i] = tmpBorrows[i];
                }
                //最后将数据有效个数更新
                borrowCount = tmpBorrows.length;
            } else {
                //将数据拷贝进去
                for (int i = 0; i < tmpBorrows.length; i++) {
                    borrowInfos[i] = tmpBorrows[i];
                }
                //最后将数据有效个数更新
                borrowCount = tmpBorrows.length;
            }
        }
    }

    /**
     * 存储借阅信息
     */
    private void torageAllBorrowInfo() {
        AnalyzingBorrowBook.storeBorrowInfo(borrowInfos, Constant.BORROW_INFO_FILE_NAME);
    }
    @Override
    public String toString() {
        return "NormalUser{" +
                "name='" + name + '\'' +
                ", userID=" + userID +
                ", role='" + role + '\'' +
                '}';
    }

    //普通用户的菜单
    @Override
    public int menu() {
        System.out.println("普通用户" + name + "的操作菜单");
        System.out.println("1. 查找图书");
        System.out.println("2. 打印所有的图书");
        System.out.println("3. 退出系统");
        System.out.println("4. 借阅图书");
        System.out.println("5. 归还图书");
        System.out.println("6. 查看当前个人借阅情况");
        System.out.println("请选择你的操作：");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }

    /**************普通用户特有的方法***********/
    //管理员和普通用户特有的方法到时候放在Library中
    //4.借阅图书
    public void borrowBook() {
        System.out.println("目前书籍如下：" );
        library.displayBooks();
        System.out.println("请输入你要借阅的图书ID：");
        int bookId = scanner.nextInt();
        scanner.nextLine();

        //检查如果没有书籍，就不能借阅
        if(library.getBookCount() == 0) {
            System.out.println("书架没有书籍暂且不能借阅");
            return;
        }

        //加载已借阅过的图书信息
        loadAllBorrowInfo();

        Book book = library.searchById(bookId);
        if(book == null) {
            System.out.println("没有你要借阅的书籍....");
            return;
        }

        //检查当前要借阅的书 是否 之前被自己 或者 别人借阅过
        for (int i = 0; i < borrowCount; i++) { //borrowCount借阅信息数组中的有效个数
            BorrowInfo borrowInfo = borrowInfos[i];
            //找到了对应的书籍
            if (borrowInfo.getBookId() == book.getBookId()) {
                if (borrowInfo.getUserId() == getUserID()) {
                    System.out.println("该书已经被你借阅过了，你的ID是：" + getUserID());
                    return;
                } else {
                    System.out.println("该书已经被其他人借阅过了，他的ID是：" + borrowInfo.getUserId());
                    return;
                }
            }
        }

        //书籍信息
        library.borrowBook(bookId);

        //封装借阅信息的对象写到 借阅表当中
        BorrowInfo borrowInfo = new BorrowInfo(userID, book.getBookId());
        borrowInfos[borrowCount] = borrowInfo;
        borrowCount++;

        //存储借阅图书信息
        torageAllBorrowInfo();

        System.out.println("借阅成功！");

    }
    //5.归还图书
    public void returnBook() {
        System.out.println("目前书籍如下：" );
        library.displayBooks();
        System.out.println("请输入你要归还的图书ID：");
        int bookId = scanner.nextInt();

        //1. 检查书架上如果不允许存书籍，就不能借阅 ==》就是书架books为空 里面放不下数据了，你还不了
        if(library.getBookCount() == 0) {
            System.out.println("书架没有书籍暂且不能归还");
            return;
        }

        //加载已借阅的图书信息
        loadAllBorrowInfo();

        Book book = library.searchById(bookId);
        if(book == null) {
            System.out.println("没有你要归还的书籍....");
            return;
        }


        for (int i = 0; i < borrowCount; i++) {
            //如果2本书的ID相同则认为是同一本书
            if (borrowInfos[i].getBookId()==book.getBookId()) {
                //且借阅用户也一样
                if (getUserID() == borrowInfos[i].getUserId()) {

                    library.returnBook(bookId);

                    System.out.println("图书 '" + book.getTitle() + "' 已成功归还。");
                    // 用最后一本替换归还的书
                    borrowInfos[i] = borrowInfos[borrowCount - 1];
                    // 清空最后一个
                    borrowInfos[borrowCount - 1] = null;
                    borrowCount--;
                    torageAllBorrowInfo();
                }else{
                    System.out.println("该书籍不是你借阅的书籍，不能归还："+book.getTitle());
                }
                return;
            }
        }
        System.out.println("你没有借阅该书籍，不需要归还，书籍ID为："+bookId);
    }
    //6.查看当前个人借阅情况
    public void viewBorrowBooks() {
        //读取当前借阅所有用户的借阅书籍的情况
        loadAllBorrowInfo(); //加在最新借阅信息
        System.out.println("您的借阅情况如下：");
        System.out.println("===========================");
        if (borrowCount == 0) {
            System.out.println("目前没有借阅记录.....");
        } else {
            boolean flg = false;
            for (int i = 0; i < borrowCount; i++) {
                //这里只能查看属于自己借阅的情况
                //用户ID相同的情况下，使用书籍ID查询书籍
                if(borrowInfos[i].getUserId() == userID) {
                    flg = true;
                    Book book = library.searchById(borrowInfos[i].getBookId());
                    System.out.println(book);
                }
            }
            if(!flg) {
                System.out.println("你没有借阅过书籍！");
            }
        }
        System.out.println("===========================");
    }
    /***************end**********************/
}
